//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#pragma package(smart_init)
#pragma resource "*.dfm"

//---------------------------------------------------------------------------
#include "vSCopyVersion.h"
#pragma link "CComboBx"
#pragma link "CEdit"
//---------------------------------------------------------------------------
//          CONSTRUCTEUR
//---------------------------------------------------------------------------
__fastcall TCopyVersion::TCopyVersion(const AnsiString &asLogNom,
                                      const int &nLogCode,
                                      const AnsiString &asVerNom,
                                      const int &nVerCode,
                                      const bool &bMenu,
                                      TComponent* Owner)
    : TForm(Owner)
{
    try {
        dmWebHelp = new TdmWebHelp((TComponent*)0);
        if(bMenu) {
            pMenuBouton->Visible = true;
            AjouterLog();
        }else {
            pMenuBouton->Visible = false;
            m_nLogCode = nLogCode;
            m_nVerCode = nVerCode;

            cLogiciel->Text  = asLogNom;
            cVerSource->Text = asVerNom;
            AnsiString asNomVersionDest(EmptyStr);
            int i = GetMaxVersion( m_nLogCode );
            cVerCible->Text = asNomVersionDest.sprintf( "%03d", ++i );
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
__fastcall TCopyVersion::~TCopyVersion()
{
    try {
        if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//A partir de la fenetre ParamVersion
//****************************************************************************//
//---------------------------------------------------------------------------
//       Vérifier l'existance d'une version par rapport à un logiciel
//---------------------------------------------------------------------------
bool __fastcall TCopyVersion::VerifVerNom(const AnsiString &asVerNom, const int &nLogCode)
{
    bool bRetour = false;
    TZZMySqlQuery * MaRequete = NULL;
    try {
        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        //Insertion dans la table référence
        //---------------------------------
        MaRequete->Sql->Text = (AnsiString) "select ver_nom "+
                                            " from versions "+
                                            "where ver_nom = "+QuotedStr(asVerNom)+
                                            " and log_code = "+nLogCode;
        MaRequete->Open();
        MaRequete->First();

        if(!asRequete(0).IsEmpty())
            bRetour = true;

        MaRequete->Sql->Clear();
        MaRequete->Close();

    }catch ( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        bRetour = false;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    return bRetour;
}
//---------------------------------------------------------------------------
//       Obtenir la derniere version par rapport à un logiciel
//---------------------------------------------------------------------------
int  __fastcall TCopyVersion::GetMaxVersion( const int &nLogCode )
{
    int nMaxVersion = 0;
    TZZMySqlQuery * MaRequete = NULL;
    try {

        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        //Insertion dans la table référence
        //---------------------------------
        MaRequete->Sql->Text = (AnsiString)"select max(ver_nom) from versions where log_code = "+nLogCode;
        MaRequete->Open();
        MaRequete->First();

        nMaxVersion = asRequete(0).ToInt();

        MaRequete->Sql->Clear();
        MaRequete->Close();

    }catch ( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        nMaxVersion = 0;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    return nMaxVersion;
}
//---------------------------------------------------------------------------
//              INSERER DANS LA TABLE VERSION
//---------------------------------------------------------------------------
void __fastcall TCopyVersion::InsererTableVersion()
{
    try {
        dmWebHelp->tVersions->Active = true;
        dmWebHelp->tVersions->Insert();
        ntVersions(LOG_CODE) = m_nLogCode;
        astVersions(VER_NOM) = cVerCible->Text;
        dmWebHelp->tVersions->Post();
        dmWebHelp->trWebHelp->Commit();
    }catch ( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//              Récupérer l id du dernier enregistrement
//---------------------------------------------------------------------------
int  __fastcall TCopyVersion::GetMaxID( void )
{
    int GenId = 0;
    TZZMySqlQuery * MaRequete = NULL;
    try {

        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        //Insertion dans la table référence
        //---------------------------------
        MaRequete->Sql->Text = "select max(ver_code) from versions";
        MaRequete->Open();
        MaRequete->First();

        GenId = nRequete(0);

        MaRequete->Sql->Clear();
        MaRequete->Close();
    }catch ( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        GenId = 0;}
    return GenId;
}
//---------------------------------------------------------------------------
//          INSERER DANS LA TABLE REFERENCE
//---------------------------------------------------------------------------
void __fastcall TCopyVersion::InsererTableRefer(const int &nVerCodeCible)
{
    TZZMySqlQuery * MaRequete = NULL;
    try {

        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        //Insertion dans la table référence
        //---------------------------------
        MaRequete->Sql->Text = (AnsiString) "select ref_nom,"+         //0
                                                   "ref_sous_nom,"+    //1
                                                   "ref_commentaire,"+ //2
                                                   "ref_adresse,"+     //3
                                                   "ref_attente "+     //4
                                            "from refer where log_code="+m_nLogCode+" and ver_code="+m_nVerCode;
        MaRequete->Open();
        MaRequete->First();
        dmWebHelp->tRefer->Active = true;

        while(!MaRequete->Eof) {
            dmWebHelp->tRefer->Insert();
            ntRefer(VER_CODE)            = nVerCodeCible;
            ntRefer(LOG_CODE)            = m_nLogCode;
            astRefer(REF_NOM)            = asRequete(0);
            astRefer(REF_SOUS_NOM)       = asRequete(1);
            astRefer(REF_COMMENTAIRE)    = asRequete(2);
            astRefer(REF_ADRESSE)        = asRequete(3);
            ntRefer(REF_ATTENTE)         = nRequete(4);
            dmWebHelp->tRefer->Post();
            dmWebHelp->trWebHelp->Commit();
            MaRequete->Next();
        }

        MaRequete->Sql->Clear();
        MaRequete->Close();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
}
//---------------------------------------------------------------------------
//                      Valider la copie
//---------------------------------------------------------------------------
void __fastcall TCopyVersion::bbOkClick(TObject *Sender)
{
    CHistorique * Hist = NULL;
    try{
        if(!Trim(cVerCible->Text).IsEmpty()) {
            if(!VerifVerNom(cVerCible->Text, m_nLogCode)){
                InsererTableVersion();
                InsererTableRefer(GetMaxID());

                Hist = new CHistorique();
                if(pMenuBouton->Visible == true)
                    Hist->AjouterOperation(Histo_DUPLI_VER, cbLogiciel->Items->Strings[cbLogiciel->ItemIndex],cbVersion->Items->Strings[cbVersion->ItemIndex] );
                else
                    Hist->AjouterOperation( Histo_DUPLI_VER, cLogiciel->Text, cVerSource->Text);
            
            }else {
                MessageDlg("Version déjà existante avec ce logiciel !", mtWarning, TMsgDlgButtons() << mbOK, 0);
            }
        }else {
            MessageDlg("Veuillez remplir tous les champs", mtWarning, TMsgDlgButtons() << mbOK, 0);
        }
    }catch ( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(Hist) {delete Hist; Hist = NULL;}
}
//---------------------------------------------------------------------------

//A partir du menu
//****************************************************************************//
void __fastcall TCopyVersion::AjouterLog()
{
    TZZMySqlQuery * MaRequete = NULL;
    try{
        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        //Insertion dans la table référence
        //---------------------------------
        MaRequete->Sql->Text = "select log_nom, log_code from logiciels";
        MaRequete->Open();
        MaRequete->First();

        while(!MaRequete->Eof) {
            cbLogiciel->Items->AddObject( asRequete(0), (TObject*)nRequete(1) );
            MaRequete->Next();
        }

        MaRequete->Sql->Clear();
        MaRequete->Close();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
}
//---------------------------------------------------------------------------
void __fastcall TCopyVersion::cbLogicielChange(TObject *Sender)
{
    TZZMySqlQuery * MaRequete = NULL;
    try{
        int idx = cbLogiciel->ItemIndex;
        if( idx != AUCUNE_SELECTION) {
            cbVersion->Items->Clear();
            int nLogiciel = (int)cbLogiciel->Items->Objects[idx];
            MaRequete = new TZZMySqlQuery( (TComponent*)0);
            MaRequete->Database = dmWebHelp->dbWebHelp;

            //Insertion dans la table référence
            //---------------------------------
            MaRequete->Sql->Text = (AnsiString)"select ver_nom, ver_code from versions where log_code ="+nLogiciel;
            MaRequete->Open();
            MaRequete->First();

            while(!MaRequete->Eof) {
                cbVersion->Items->AddObject( asRequete(0), (TObject*)nRequete(1) );
                MaRequete->Next();
            }

            MaRequete->Sql->Clear();
            MaRequete->Close();

            cVerCible->Clear();
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
}
//---------------------------------------------------------------------------
void __fastcall TCopyVersion::cbVersionChange(TObject *Sender)
{
    try{
        AnsiString asNomVersionDest(EmptyStr);
        int nIdxLog = cbLogiciel->ItemIndex;
        int nIdxVer = cbVersion->ItemIndex;
        if( (nIdxLog != AUCUNE_SELECTION) && (nIdxVer != AUCUNE_SELECTION) ) {

            m_nLogCode = (int)cbLogiciel->Items->Objects[cbLogiciel->ItemIndex];
            m_nVerCode = (int)cbVersion->Items->Objects[cbVersion->ItemIndex];

            int i = GetMaxVersion( m_nLogCode );
            cVerCible->Text = asNomVersionDest.sprintf( "%03d", ++i );
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
